package sm.hibernate.type;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Date;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;

import sm.codec.type.NullValue;
import sm.codec.type.RC;

public class TimeType extends AbstractPrimitiveType {

	@Override
	public int[] sqlTypes() {
		return new int[] { Types.TIMESTAMP };
	}

	@Override
	public Class returnedClass() {
		return Date.class;
	}

	@Override
	public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
			throws HibernateException, SQLException {
		Object value = Hibernate.TIMESTAMP.nullSafeGet(rs, names);
		if (value == null) {
			value = NullValue.TimeNull;
		}
		return value;
	}

	@Override
	public void nullSafeSet(PreparedStatement st, Object value, int index)
			throws HibernateException, SQLException {
		if (value instanceof NullValue || value instanceof RC) {
			value = null;
		}
		Hibernate.TIMESTAMP.nullSafeSet(st, value, index);
	}

}
